<?php
/**
 * Boot application
 * 
 * @version 0.1
 * @author Sergey Tihonov
 */
namespace Application;
class Boot
{
    /**
     * An array of options for application
     *
     * @var array
     */
    protected $_config = array();
    
    /**
     * @var \Engine\Request
     */
    protected $_request = null;
    
    /**
     * @var \Engine\Response
     */
    protected $_response = null;
    
    /**
     * @var \Engine\Router 
     */
    protected $_router = null;
    
    /**
     * @var \Engine\View
     */
    protected $_view = null;

    /**
     * Constructor bootloader
     * Set config file and engine start
     *
     * @return void
     */
    public function __construct()
    {
        // real path for config file
        $config_real_path = APPLICATION_DIR . '/config.ini';
        if (!is_file($config_real_path)) {
            throw new \Exception('Configuration file is not found in dir ' . APPLICATION_DIR);
        }
        
        $this->_config = parse_ini_file($config_real_path, true);

        // include engine classes
        require ENGINE_DIR . '/Request.php';
        $this->_request = new \Engine\Request();
        
        require ENGINE_DIR . '/Response.php';
        $this->_response = new \Engine\Response();
        
        require ENGINE_DIR . '/Router.php';
        $this->_router = new \Engine\Router();
    }
    
    /**
     *
     * @param string $command
     * @throws \Exception 
     * @return string
     */
    public function executeAction($command)
    {
        $path_to_action = APPLICATION_DIR . '/modules/' . strtolower(strtr($command, array('.' => '/'))) . '.php';
        if (!is_file($path_to_action)) {
            throw new \Exception('Action is not found in system');
        }
        return $path_to_action;
    }
    
    /**
     * @return \Engine\Request
     */
    public function getRequest()
    {
        return $this->_request;
    }
    
    /**
     * @return \Engine\Response
     */
    public function getResponse()
    {
        return $this->_response;
    }
    
    /**
     * @return \Engine\Router
     */
    public function getRouter()
    {
        return $this->_router;
    }
    
    /**
     * @return \Engine\View
     */
    public function getView()
    {
        if ($this->_view === null) {
            require ENGINE_DIR . '/View.php';
            $this->_view = new \Engine\View();
        }
        
        return $this->_view;
    }
    
    /**
     * Add route for method GET
     * 
     * @param string $route
     * @param string $path
     * @param array $paramsType
     */
    public function get($route, $path, $paramsType = array())
    {
        $this->_router->addRoute('GET', $route, $this->executeAction($path), $paramsType);
    }
    
    /**
     * Add route for method POST
     * 
     * @param string $route
     * @param string $path
     * @param array $paramsType
     */
    public function post($route, $path, $paramsType = array())
    {
        $this->_router->addRoute('POST', $route, $this->executeAction($path), $paramsType);
    }
    
    /**
     * Run application 
     */
    public function run()
    {
        require ENGINE_DIR . '/Dispatcher.php';
        \Engine\Dispatcher::run($this);
    }
}
